package com.alex.server.service.impl;

import com.alex.server.pojo.RespBean;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.alex.server.mapper.DepartmentMapper;
import com.alex.server.pojo.Department;
import com.alex.server.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author alex
 * @since 2022-08-30
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    public List<Department> getAllDepartments() {
        return departmentMapper.getAllDepartment(-1);

    }
    /**
     * 添加部门
     * @param dep
     * @return
     */
    @Override
    public RespBean addDep(Department dep) {
        dep.setEnabled(true);
        departmentMapper.addDep(dep);
        if (dep.getResult()==1) {
            return RespBean.success("添加成功");
        }else{
            return RespBean.error("添加失败");
        }
    }

    /**
     * 根据id删除部门
     * @param
     * @return
     */
    @Override
    public RespBean deleteDep(Integer id) {
        Department dep=new Department();
        dep.setId(id);
        departmentMapper.deleteDep(dep);
        if (dep.getResult()==-1) {
            return RespBean.error("该部门下有员工,无法删除");
        }else if (dep.getResult()==-2){
            return RespBean.error("该部门下有子部门,请先删除子部门");
        }else if (dep.getResult()==1){
            return RespBean.success("删除成功");
        }else {
            return RespBean.error("删除失败");
        }
    }

}
